package com.example.dense_rack_server.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dense_rack_server.entity.MyDept;
import com.example.dense_rack_server.entity.MyUser;
import com.example.dense_rack_server.service.MyDeptService;
import com.example.dense_rack_server.service.MyUserService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-03
 */
@Controller
@RequestMapping("/my/dept")
public class MyDeptController {
    @Resource
    private MyDeptService myDeptService;
    @Resource
    public MyUserService myUserService;

    @GetMapping("index")
    public String index() {
        return "dept/dept-list";
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> deptList() {
        return myDeptService.deptList();
    }

    /**
     * 部门停用和启用
     */
    @PutMapping("/status")
    @ResponseBody
    public ResponseModel updateStatus(@RequestBody JSONObject data) {
        if (StrUtil.isEmpty(data.getStr("deptId")) || StrUtil.isEmpty(data.getStr("status"))) {
            return ResponseModel.fail("缺少参数");
        }
        return myDeptService.updateStatus(data);
    }

    /**
     * 新增部门页面
     *
     * @return
     */
    @GetMapping("/add/view")
    public ModelAndView addDept() {
        ModelAndView modelAndView = new ModelAndView("dept/dept-add");
        modelAndView.addObject("userList", myUserService.list(
                new QueryWrapper<MyUser>().eq("status", 1)
        ));
        return modelAndView;
    }

    /**
     * 新增部门保存
     */
    @PostMapping("/add/save")
    @ResponseBody
    public ResponseModel addDept(@RequestBody JSONObject data) {
        if (StrUtil.isEmpty(data.getStr("deptName")) || StrUtil.isEmpty(data.getStr("sort"))) {
            return ResponseModel.fail("缺少参数");
        }
        return myDeptService.addDept(data);
    }


    /**
     * 绘制部门树
     */
    @GetMapping("/buildDeptAll")
    @ResponseBody
    public ResponseModel buildDeptAll() {
        return myDeptService.buildDeptAll();
    }

    /**
     * 编辑部门信息页面
     *
     * @param deptId
     * @return
     */
    @GetMapping("/edit/view")
    public ModelAndView editDept(@RequestParam(value = "deptId", required = true) String deptId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dept/dept-edit");
        MyDept myDept = myDeptService.getById(deptId);
        modelAndView.addObject("myDept", myDept);
        modelAndView.addObject("userList", myUserService.list(
                new QueryWrapper<MyUser>().eq("status", 1)
        ));
        return modelAndView;
    }

    /**
     * 修改部门信息保存
     *
     * @param data
     * @return
     */
    @PostMapping("/edit/save")
    @ResponseBody
    public ResponseModel editDept(@RequestBody JSONObject data) {
        return myDeptService.editDept(data);
    }

    /**
     * 部门详情
     */
    @GetMapping("/detail/{dept_id}")
    public ModelAndView deptDetail(@PathVariable("dept_id") String dept_id) {
        return myDeptService.deptDetail(dept_id);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("delete")
    @ResponseBody
    public ResponseModel deleteDept(@RequestParam(value = "deptId",required = true) int deptId){
        return myDeptService.deleteDept(deptId);
    }
}
